$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.form = new xhForm($('#xh_form')).parse();
            if(info != null){
                this.form.fill(info);
            }
            this.form.bindRequired($('#calculate'));
            $(".form_date").datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month", autoclose:true});
            this.listener();
        },
        listener:function () {
            var $this = this;
            //点击事件
            $('#bt_modal_confirm',parent.document).click(function () {
               $this.funObj().submitForm();
            });
            //计算
            $('#calculate').click(function () {
                $this.funObj().calculate();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                submitForm:function () {
                    $this.form.submit('lm/rent/savePay',{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame()['tabFrame']['rentPayFrame'].obj.dataGrid.reload();
                        window.top.xhTop.closeModal();
                    });
                },
                calculate:function () {
                    var startTime = $('[name=tStartDate]').val();
                    var endTime   = $('[name=tEndDate]').val();
                    var monthPay  = $('[name=iMonthPay]').val();
                    var cashPledge= $('[name=iCashPledge]').val(); //押金
                    var agencyFee = $('[name=iAgencyFee]').val(); //中介费
                    var tax       = $('[name=iTaxes]').val() ;  //税费
                    var months = MonthDiff(startTime,endTime);
                    cashPledge = (cashPledge =='') ? 0 : cashPledge;
                    agencyFee  = (agencyFee =='') ? 0 : agencyFee;
                    tax        = (tax =='') ? 0 : tax;
                    var total = months*parseInt(monthPay) + parseInt(cashPledge) + parseInt(agencyFee) + parseInt(tax);
                    $('[name=iTotal]').val(total);
                }

            }
        }
    }
};

//计算天数差的函数，通用
function  MonthDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式
    sDate1 = sDate1.split('-');
// 得到月数
    var date1 = parseInt(sDate1[0]) * 12 + parseInt(sDate1[1]);
// 拆分年月日
    sDate2 = sDate2.split('-');
    var date2 = parseInt(sDate2[0]) * 12 + parseInt(sDate2[1]);
    var dateDiff = Math.abs(date1 - date2);
    if(parseInt(sDate1[2]) <= parseInt(sDate2[2]) ){
        dateDiff = dateDiff +1;
    }
    return dateDiff;
}